import React from 'react';
import moment from 'moment';
import { Icon } from 'antd';
import Link from 'umi/link';

const parseJsonToParams = data => {
  let str = '';
  for (let key in data) {
    if (typeof data[key] !== 'undefined' && data[key] !== undefined && data[key] !== '' && data[key] !== 'null' && data[key] !== null) {
      str += `${key}=${data[key]}&`
    }
  }
  return str;
}

const formatParams = obj =>{
  let temp = {};
  for (let i in obj) {
    if ((obj[i] + "").replace(/\s+/g, "") !== "" || obj[i] === null) {
      temp[i] = obj[i];
    }
  }
  return temp
}

const formatDate = (time,format='YYYY-MM-DD') =>{
  return moment(JSON.parse(time)).format(format)
}

const exitWindowOpen = () =>
{
    window.opener = null;
    window.open('', '_self', '');
    window.close();//以上三行可关闭单个页面
    window.open('', '_top');
    window.top.close();
    window.location.href = 'about:blank ';
    window.close();//上面两次关闭适用于FireFox等浏览器
}

const openWindow = (url) => {
  var tempwindow = window.open('about:blank')
  tempwindow.location = url;
}

//审核进度的状态
const StatusIcon = ({ status, text }) => (
  <span>
    <Icon
      type={(status === 'success' || status === 'unrealName') ? "check-circle" : 'close-circle'}
      style={{
        color: status === 'success' ? '#52C41A' : status === 'fail' ? 'red' : '#666', marginRight: 5
      }} />
    {text}
  </span>
);

//展示实名认证审核的进度
const isBindInfo = (status, isShow=true) => {
  if (status === 0) {
    return (
      <span>
        <StatusIcon status="unrealName" text="未实名认证" />
        {
          isShow && <Link to={`/userCenter/authentication?typeName=${'creat'}`}> 认证 </Link>
        }
      </span>
    )
  } else if (status === 1) {
    return (
      <span>
        <StatusIcon status="unrealName" text="实名认证审核中" />
      </span>
    )
  } else if (status === 2) {
    return (
      <span>
        <StatusIcon status="success" text="已实名认证" />
        {
          isShow && <Link to={`/userCenter/authentication?typeName=${'creatNew'}`}> 重新认证 </Link>
        }
      </span>
    )
  } else if (status === 3) {
    return (
      <span>
        <StatusIcon status="fail" text="实名认证未通过" />
        {
          isShow && <Link to={`/userCenter/authentication?typeName=${'creatNew'}`}> 重新认证 </Link>
        }
      </span>
    )
  }
}

const handleBeforeUpload = (file, type=["png", "jpeg", "jpg", "bmp"], size=5) =>{
  //TODO: 不能直接返回false，因为显示框里面还是会存在上传的值
  return new Promise((resolve, reject) => {
    const name = file.name;
    const seat = name.lastIndexOf(".")+1;
    const extension = name.substring(seat).toLowerCase();
    //const isType = type.some(e => file.type.indexOf(e) !== -1);
    const isType = type.some(e => extension.indexOf(e) !== -1);
    const isLt5M = file.size / 1024 / 1024 <= size;
    if (!isType) {
      message.error('上传文件类型不正确！');
      reject();
    }else if(!isLt5M){
      message.error('文件必须小于5MB!');
      reject();
    }else{
      resolve(file);
    }
  });
}


const banWindowBack = () =>{
    //防止页面后退
    if (window.history && window.history.pushState) {
        window.history.pushState('forward', null, '#');
        window.history.forward(1);
    }
    window.history.pushState('forward', null, '#'); //在IE中必须得有这两行
    window.history.forward(1);
    //history.pushState(null, null, document.URL);
}

export {
  parseJsonToParams,
  formatParams,
  formatDate,
  exitWindowOpen,
  openWindow,
  isBindInfo,
  StatusIcon,
  handleBeforeUpload,
  banWindowBack,
}
